#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          strigger --set --reconfig
############################################################################
# Copyright (C) 2007 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set file_in      "test$test_id.input"
set file_out     "test$test_id.output"

# TODO: Temporary variables to debug bug 9665 (remove once fixed)
set trigger_get_failed false
set config_dir [get_conf_path]

set uid [get_my_uid]
set user [get_my_user_name]
if {![regexp "${user}\\($number\\)" [get_config_param "SlurmUser"]] || $user eq "root"} {
	skip "This test must run as the SlurmUser but this user cannot be root"
}

#
# Build input script file
#
set cwd "[$bin_pwd]"
exec $bin_rm -f $file_in $file_out
make_bash_script $file_in "$bin_echo RECONFIG >$cwd/$file_out"

proc cleanup {} {
	global bin_rm file_in file_out strigger uid

	# TODO: Temporary debug for bug 9665 (remove once fixed)
	global trigger_get_failed
	if {$trigger_get_failed} {
		if [
			wait_for -timeout 5 -poll .1 {
				regexp "$file_in" [run_command_output "$strigger --get -v --reconfig --user=$uid"]
			} {}
		] {
			log_warn "Trigger get failed and never recovered"
		} else {
			log_warn "Trigger get failed but became available afterwards"
		}
	}
	global config_dir scontrol
	restore_conf $config_dir/slurm.conf
	run_command "$scontrol reconfigure"

	run_command -none -nolog "$strigger --clear --reconfig --user=$uid --quiet"
	exec $bin_rm -f $file_in $file_out
}

# TODO: Temporarily increase logging to debug bug 9665 (remove once fixed)
save_conf $config_dir/slurm.conf
run_command -none "$bin_echo SlurmctldDebug=debug3 >> $config_dir/slurm.conf"
run_command -none "$bin_echo DebugFlags=Triggers >> $config_dir/slurm.conf"
run_command -fail "$scontrol reconfigure"
wait_for -timeout 5 -pollinterval .1 {[get_config_param "SlurmctldDebug"] eq "debug3"} {}
wait_for -timeout 5 -pollinterval .1 {[get_config_param "DebugFlags"] eq "Triggers"} {}

run_command -fail "$strigger --clear --quiet --user=$uid"

# Create a trigger on a reconfig event
if {![regexp "trigger set" [run_command_output -fail "$strigger --set -v --offset=0 --reconfig --program=$cwd/$file_in"]]} {
	fail "Trigger creation failure"
}

# Verify that the trigger has been created
if {![regexp "$file_in" [run_command_output -fail "$strigger --get -v --reconfig --user=$uid"]]} {
	# TODO: Temporary variable to debug bug 9665 (remove once fixed)
	set trigger_get_failed true
	fail "Trigger get failure"
}

#
# Try to trigger event
#
set invalid 0
spawn $scontrol reconfig
expect {
	-re "Invalid user" {
		set invalid 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$invalid == 0} {
	# Add delay for slurmctld to process triggers (every 15 secs)
	exec sleep 15

	# Plus time for cross-platform NSF delays
	if {[wait_for_file $file_out] != 0} {
		fail "File $file_out is missing"
	} else {
		set reconfig 0
		spawn $bin_cat $file_out
		expect {
			-re "RECONFIG" {
				set reconfig 1
				exp_continue
			}
			eof {
				wait
			}
		}
		if {$reconfig == 0} {
			fail "File $file_out contents are bad"
		}
	}
}
